import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Game } from "../Game";
import { GameConst } from "../../component/gconst/GameConst";
import { MapIdRule } from "../rules/MapIdRule";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { CrossTaskHintBtn } from "../view/crossTask/CrossTaskHintBtn";
import { SCrossSpyOpen } from "../../../Message/Public/SCrossSpyOpen";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { CrossTaskWin } from "../view/crossTask/CrossTaskWin";
import { GameProxy } from "../mvc/GameProxy";
import { SCrossCountryInfo } from "../../../Message/Game/SCrossCountryInfo";
import { CrossTaskTaskDescData } from "../view/crossTask/data/CrossTaskTaskDescData";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { TaskConfig } from "../resource/TaskConfig";
import { DataProvider } from "../../../fl/data/DataProvider";
type int = number;
//class CrossTaskController
    
    export  class CrossTaskController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSpyOpen, this.openHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSpyClose, this.closeHandler);
            Dispatcher.addEventListener(EventName.CrossTaskShowModule, this.showModuleHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            Dispatcher.addEventListener(EventName.CrossPVP_getServerListSuccess, this.serverListHandler);
            Dispatcher.addEventListener(EventName.CrossTaskSelectServer, this.serverSelectedHandler);
            Dispatcher.addEventListener(EventName.CrossTaskSelectGate, this.seletedGateHandler);
            Dispatcher.addEventListener(EventName.CrossTaskLastFinishTaskCodeUpdate, this.lastCodeUpdateHandler);
            Dispatcher.addEventListener(EventName.CrossTaskGetTaskReq, this.getTaskReqHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDel, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListRefresh, this.taskUpdateHandler);
            return;
        }

        /* internal  */sceneUpdateHandler(arg1: DataEvent): void
        {
            var loc1=!GameMapUtil.curMapState.isCrossCountry;
            Game.scene.updateNpcIsAddToStage(GameConst.CountryMoneyNpcId, loc1, MapIdRule.TaijiCity);
            if (GameMapUtil.curMapState.isCrossCountry && !(this._win == null) && !this._win.isHide) 
            {
                this._win.hide();
            }
            return;
        }

        /* internal  */openHandler(arg1: MessageBlock): void
        {
            cache.crossTask.isInCrossTask = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryBuffSkillOpened));
            if (cache.role.roleInfo.level < LevelLimitDefin.CrossTaskGetLevel) 
            {
                return;
            }
            if (this._icon == null) 
            {
                this._icon = new CrossTaskHintBtn();
            }
            this._icon.updateCountDown(arg1.messageBase as SCrossSpyOpen);
            this._icon.show();
            return;
        }

        /* internal  */closeHandler(arg1: MessageBlock): void
        {
            cache.crossTask.isInCrossTask = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryBuffSkillClose));
            if (this._icon && !this._icon.isHide) 
            {
                this._icon.hide();
                this._icon.dispose(true);
                this._icon = null;
            }
            return;
        }

        /* internal  */showModuleHandler(arg1: DataEvent): void
        {
            if (cache.role.roleInfo.level < LevelLimitDefin.CrossTaskGetLevel) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(80478, LevelLimitDefin.CrossTaskGetLevel));
                return;
            }
            if (this._win == null) 
            {
                this._win = new CrossTaskWin();
                this._win.configEventListener(flash.events.Event.ADDED_TO_STAGE, this.moudleAddToStageHandler);
            }
            this._win.show();
            return;
        }

        /* internal  */moudleAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this.updateTaskShow();
            GameProxy.country.getCrossCountryList();
            return;
        }

        /* internal  */updateTaskShow(): void
        {
            if (this._win == null) 
            {
                return;
            }
            var loc1=cache.crossTask.getTaskData();
            this._win.taskList = loc1;
            return;
        }

        /* internal  */serverSelectedHandler(arg1: DataEvent): void
        {
            cache.crossTask.curServer = arg1.data as SCrossCountryInfo;
            return;
        }

        /* internal  */seletedGateHandler(arg1: DataEvent): void
        {
            if (cache.crossTask.curServer == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80467));
                return;
            }
            var loc1=int(arg1.data);
            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossPVP_enterCrossCountry, {"countryKey": cache.crossTask.curServer, "pointType": loc1}));
            return;
        }

        /* internal  */lastCodeUpdateHandler(arg1: DataEvent): void
        {
            this.updateTaskShow();
            return;
        }

        /* internal  */getTaskReqHandler(arg1: DataEvent): void
        {
            if (cache.role.roleInfo.level < LevelLimitDefin.CrossTaskGetLevel) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80472));
                return;
            }
            var loc1=arg1.data as CrossTaskTaskDescData;
            if (loc1 == null) 
            {
                return;
            }
            if (loc1.status != ETaskStatus._ETaskStatusCanGet) 
            {
                return;
            }
            var loc2=TaskConfig.instance.getTaskConfigByCode(loc1.taskCode);
            if (loc2 == null) 
            {
                return;
            }
            GameProxy.taskProxy.getTaskReq(loc2.getNpc, loc2.code);
            return;
        }

        /* internal  */taskUpdateHandler(arg1: Object): void
        {
            if (this._win == null || this._win.isHide) 
            {
                return;
            }
            this.updateTaskShow();
            return;
        }

        /* internal  */serverListHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            loc1.sort(this.sortServerList);
            var loc2=new DataProvider(loc1);
            if (this._win == null) 
            {
                return;
            }
            this._win.serverList = loc2;
            return;
        }

        /* internal  */sortServerList(arg1: SCrossCountryInfo, arg2: SCrossCountryInfo): int
        {
            if (arg1.money < arg2.money) 
            {
                return 1;
            }
            return -1;
        }

        private /* var */_icon: CrossTaskHintBtn;

        private /* var */_win: CrossTaskWin;
    }
